package cn.com.dhc.danlu.activity.coupon;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.base.DanluTabFragmentActivity;
import cn.com.dhc.danlu.activity.base.WebViewActivity;
import cn.com.dhc.danlu.constant.UrlConst;
import cn.com.dhc.danlu.dlware.OperatorUtils;
import cn.com.dhc.danlu.dlware.net.GeneralBusinessObserver;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.TipCountUtil;
import cn.com.dhc.danlu.widget.couponview.CouponMorePopupView;
import cn.com.dhc.danlu.widget.couponview.filterview.FilterType;
import cn.com.hd.mall.web.webservices.entity.freedom.request.coupon.GetCouponCntReq;
import cn.com.hd.mall.web.webservices.entity.freedom.response.coupon.GetCouponCntResponse;
import rx.Subscription;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * @author: 李一方
 * @time: 2016-09-22 14:09
 * @mail: liyifang@danlu.com
 * @desc: 经销商红包 & 我的优惠券
 */
public class DealersCouponUsedActivity extends DanluTabFragmentActivity {
    private int popWidth;

    private FilterType mFilterType;
    private Subscription mSubscription;
    private CouponMorePopupView mMorePopupView;

    public static void startSelf(Context context, FilterType type) {
        Intent intent = new Intent(context, DealersCouponUsedActivity.class);
        intent.putExtra(CommonConst.CouponConst.DEALERS_INTENT_TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dealers_coupon_used);

        initArgs(savedInstanceState);
        initViews();
        setView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mSubscription && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(CommonConst.CouponConst.DEALERS_INTENT_TYPE, mFilterType);
    }

    private void initArgs(Bundle savedInstanceState) {
        if (null != savedInstanceState) {
            mFilterType = (FilterType) savedInstanceState.getSerializable(CommonConst.CouponConst.DEALERS_INTENT_TYPE);
        } else {
            mFilterType = (FilterType) getIntent().getSerializableExtra(CommonConst.CouponConst.DEALERS_INTENT_TYPE);
        }
    }

    private void initViews() {
        if (mFilterType != null) {
            switch (mFilterType) {
                case dealers_coupon:
                    mToolbar.setTitle(R.string.dl_my_dealers_conpon);
                    break;

                case dealers_envelop:
                    mToolbar.setTitle(R.string.dealer_envelope);
                    break;

                default:
                    break;
            }
        }

        mToolbar.setRightImg(R.drawable.icon_coupon_more);
    }

    private void setView() {
        mToolbar.setRightClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mMorePopupView && mMorePopupView.isShowing()) {
                    mMorePopupView.dismiss();
                } else {
                    showMenu();
                }
            }
        });
    }


    private void showMenu() {
        if (null == mMorePopupView) {
            initPopupView();
        }

        if (popWidth == 0) {
            popWidth = mMorePopupView.getContentView().getMeasuredWidth();
        }

        int width = mToolbar.getWidth();
        int height = mToolbar.getHeight();

        switch (mFilterType) {
            case dealers_coupon:
                mMorePopupView.showAsDropDown(mToolbar, width - (int) (popWidth * 2.7f), -height / 3);
                break;

            case dealers_envelop:
                mMorePopupView.showAsDropDown(mToolbar, width - 3 * popWidth, -height / 3);
                break;
        }
    }

    private void initPopupView() {
        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        mMorePopupView = new CouponMorePopupView(this, decorView);
        mMorePopupView.setNeedAnim(false);
        mMorePopupView.setOutsideTouchable(true);

        switch (mFilterType) {
            case dealers_coupon:
                mMorePopupView.setMenuItem(R.string.dl_title_get_conpon, R.string.coupon_description);
                break;

            case dealers_envelop:
                mMorePopupView.setMenuItem(R.string.dl_title_rob_envelop, R.string.envelope_description);
                break;

            default:
                break;
        }

        mMorePopupView.setOnMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.tv_coupon) {
                    // 领券中心 | 抢经销商红包
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(CommonConst.CouponConst.DEALERS_INTENT_TYPE, mFilterType);
                    DealersCouponObtainActivity.startSelf(DealersCouponUsedActivity.this, bundle);
                    mMorePopupView.dismiss();
                } else if (v.getId() == R.id.tv_desc) {
                    switch (mFilterType) {
                        case dealers_coupon:
                            WebViewActivity.startSelf(DealersCouponUsedActivity.this, CommonUtils.getStringByResId(R.string.dl_title_desc_coupon), UrlConst.URL_DEALER_COUPON, false);
                            break;

                        case dealers_envelop:
                            WebViewActivity.startSelf(DealersCouponUsedActivity.this, CommonUtils.getStringByResId(R.string.dl_title_desc_envelop), UrlConst.URL_DEALER_ENVELOP, false);
                            break;

                        default:
                            break;
                    }

                    mMorePopupView.dismiss();
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 界面可见的时候刷新当前数量
        getCouponCnt();
    }

    private void getCouponCnt() {
        String token = agent.getToken();
        agent.getApi()
                .getCouponCnt(new GetCouponCntReq(token, mFilterType == FilterType.dealers_coupon ? "11" : "10"))
                .compose(OperatorUtils.<GetCouponCntResponse>defaultSchedulers())
                .subscribe(new GeneralBusinessObserver<GetCouponCntResponse>() {
                    @Override
                    protected void onSuccess(GetCouponCntResponse model) {
                        updateTitle(0, String.format("未使用(%s)", TipCountUtil.cover(model.getUnUsedCnt(), TipCountUtil.BIG_FLAG)));
                        updateTitle(1, String.format("已使用(%s)", TipCountUtil.cover(model.getUsedCnt(), TipCountUtil.BIG_FLAG)));
                        updateTitle(2, String.format("已过期(%s)", TipCountUtil.cover(model.getExpireCnt(), TipCountUtil.BIG_FLAG)));
                    }
                });
    }

    @Override
    protected String[] createTitles() {
        if (mFilterType == FilterType.dealers_envelop_use) {
            return new String[]{
                    CommonUtils.getStringByResId(R.string.dl_dealers_envelop_canuse) + "(0)", CommonUtils.getStringByResId(R.string.dl_dealers_envelop_notuse) + "(0)"
            };
        } else {
            return new String[]{
                    CommonUtils.getStringByResId(R.string.myself_coupon_not_used) + "(0)", CommonUtils.getStringByResId(R.string.myself_coupon_used) + "(0)", CommonUtils.getStringByResId(R.string.myself_coupon_overdue) + "(0)"
            };
        }
    }

    @Override
    protected Fragment getFragmentByPos(int position) {
        Fragment fragment;

        switch (position) {
            case 1:
                fragment = DealersUsedFragment.newInstance(mFilterType, CommonConst.CouponConst.DEALERS_HAS_USED);
                break;

            case 2:
                fragment = DealersUsedFragment.newInstance(mFilterType, CommonConst.CouponConst.DEALERS_OUT_DATE);
                break;

            case 0:
            default:
                fragment = DealersUsedFragment.newInstance(mFilterType, CommonConst.CouponConst.DEALERS_NOT_USE);
                break;
        }

        return fragment;
    }
}